package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.util.Result;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class PasswordExistServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password= request.getParameter("password");

        AccountService accountService = new AccountService();

//        String result = "-1";
        String result = accountService.getPassWord(username);

        response.setContentType("text/json");//服务端将其作为json对象输出
        PrintWriter out = response.getWriter();

        Result result1 = new Result();

        if(!password.equals(result) )
        {   //密码可用
            result1.setCode(1);
            result1.setMsg("Not Exist");
        }
        else{
            //密码不可用
            result1.setCode(0);
            result1.setMsg("Exist");
        }
        //使用fastjson把json对象变成一个json字符串
        String str = JSON.toJSONString(result1);
        out.print(str);
        out.flush();
        out.close();
    }
}



